package com.wnhoo.smartfishtank.fishtank;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

import com.wnhoo.smartfishtank.R;
import com.wnhoo.smartfishtank.app.LoginRunnable;
import com.wnhoo.smartfishtank.base.BaseActivity;
import com.wnhoo.smartfishtank.util.ShowToastUtil;
import com.wnhoo.smartfishtank.util.UIUtil;

import cn.jpush.android.api.JPushInterface;

import static com.wnhoo.smartfishtank.app.LMAPP.exec;


public class LaunchActivity extends BaseActivity implements View.OnClickListener {
    private Button launcher_ok;
    private String TAG = "LaunchActity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ShortcutUtil.createShortCut(this, R.drawable.light_icon,
//                R.string.hello_world);
//        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_launch);
        launcher_ok = (Button) findViewById(R.id.enter);
        launcher_ok.setOnClickListener(this);
        initPermissions();
        boolean flag = UIUtil.isZh(this);
        if (flag) {
            launcher_ok.setBackgroundResource(R.drawable.enter_selector);
            ((ImageView) findViewById(R.id.launcher_bg)).setImageResource(R.drawable.therfish);
        } else {
            launcher_ok.setBackgroundResource(R.drawable.enter_en_selector);
            ((ImageView) findViewById(R.id.launcher_bg)).setImageResource(R.drawable.therfish_en);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(LaunchActivity.this, DataPlug_ListActivity.class));
        LaunchActivity.this.finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    public void initPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请READ_PHONE_STATE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                    1);
        }else{
            launcher_ok.setClickable(true);
            exec.execute(new LoginRunnable(this));
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //TODO:已授权
            launcher_ok.setClickable(true);
            exec.execute(new LoginRunnable(this));
        } else {
            //TODO:用户拒绝
            launcher_ok.setClickable(false);
            ShowToastUtil.show(getApplication(),getStringXml(R.string.number));
        }
    }
}
